/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import edu.gatech.jiggawatt.office.model.DoctorsOrder;
import edu.gatech.jiggawatt.office.model.TreatmentRecord;
import edu.gatech.jiggawatt.office.presenter.Presenter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Represents "View Edit Doctors Order Frame"
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class ViewEditDoctorsOrderFrame extends JFrame { // $codepro.audit.disable declareDefaultConstructors

	private JPanel contentPane;
	private JTextArea prescriptionArea;
	private JCheckBox chckbxBloodTest;
	private JCheckBox chckbxUrineTest;
	private JCheckBox chckbxImmunizationTest;
	private JTextArea followUpArea;
	private JTextArea otherArea;
	private Presenter presenter;
	private TreatmentRecord currentTr;
	private DoctorsOrder currentOrder;
	private String userId;
	private boolean doctor;	

	/**
	 * Create the frame.
	 * @param p presenter
	 * @param id id of patient that this doctors order belong to
	 * @param tr treatment record to view or edit
	 * @param d true if doctor is viewing this screen, false if patient is viewing this screen
	 */
	public ViewEditDoctorsOrderFrame(Presenter p, String id, TreatmentRecord tr, boolean d)
	{
		
		presenter = p;
		currentTr = tr;
		currentOrder = currentTr.getDoctorsOrder();
		userId = id;
		doctor = d;
		
		setTitle("View/Edit Doctors Order"); // $codepro.audit.disable stringLiterals
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 369, 571); // $codepro.audit.disable numericLiterals
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); // $codepro.audit.disable numericLiterals
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPrescription = new JLabel("Prescription:"); // $codepro.audit.disable stringLiterals
		lblPrescription.setBounds(25, 24, 75, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblPrescription);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 49, 292, 107); // $codepro.audit.disable numericLiterals
		contentPane.add(scrollPane);
		
		prescriptionArea = new JTextArea();
		prescriptionArea.setText(currentOrder.getPrescription());
		scrollPane.setViewportView(prescriptionArea);
		
		JLabel lblLabWorkdPerformed = new JLabel("Lab works Performed:"); // $codepro.audit.disable stringLiterals
		lblLabWorkdPerformed.setBounds(25, 179, 133, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblLabWorkdPerformed);
		
		chckbxBloodTest = new JCheckBox("Blood Test"); // $codepro.audit.disable stringLiterals
		chckbxBloodTest.setSelected(currentOrder.isBloodTest());
		chckbxBloodTest.setBounds(25, 200, 95, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(chckbxBloodTest);
		
		chckbxUrineTest = new JCheckBox("Urine Test"); // $codepro.audit.disable stringLiterals
		chckbxUrineTest.setSelected(currentOrder.isUrineTest());
		chckbxUrineTest.setBounds(122, 200, 89, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(chckbxUrineTest);
		
		chckbxImmunizationTest = new JCheckBox("Immunization Test"); // $codepro.audit.disable stringLiterals
		chckbxImmunizationTest.setSelected(currentOrder.isImmunizationTest());
		chckbxImmunizationTest.setBounds(214, 200, 133, 23); // $codepro.audit.disable numericLiterals
		contentPane.add(chckbxImmunizationTest);
		
		JLabel lblFollowup = new JLabel("Follow-up:"); // $codepro.audit.disable stringLiterals
		lblFollowup.setBounds(25, 241, 97, 14); // $codepro.audit.disable numericLiterals
		contentPane.add(lblFollowup);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(25, 266, 292, 74); // $codepro.audit.disable numericLiterals
		contentPane.add(scrollPane_1);
		
		followUpArea = new JTextArea();
		followUpArea.setText(currentOrder.getFollowUpInstruction());
		scrollPane_1.setViewportView(followUpArea);
		
		JLabel lblOtherInstruction = new JLabel("Other Instruction:"); // $codepro.audit.disable stringLiterals
		lblOtherInstruction.setBounds(25, 351, 149, 14);
		contentPane.add(lblOtherInstruction);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(25, 376, 292, 81);
		contentPane.add(scrollPane_2);
		
		otherArea = new JTextArea();
		otherArea.setText(currentOrder.getOtherInstruction());
		scrollPane_2.setViewportView(otherArea);
		
		JButton btnUpdate = new JButton("Update"); // $codepro.audit.disable stringLiterals
		btnUpdate.setBounds(57, 489, 89, 23);
		contentPane.add(btnUpdate);
		
		JButton btnBack = new JButton("Back"); // $codepro.audit.disable stringLiterals
		
		if (!doctor)
		{
			setTitle("View Doctors Order"); // $codepro.audit.disable stringLiterals
			btnUpdate.setEnabled(false);
			btnUpdate.setVisible(false);
			prescriptionArea.setEnabled(false);
			chckbxBloodTest.setEnabled(false);
			chckbxUrineTest.setEnabled(false);
			chckbxImmunizationTest.setEnabled(false);
			followUpArea.setEnabled(false);
			otherArea.setEnabled(false);			
		}
		
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				DoctorsOrder newOrder;
				
				newOrder = new DoctorsOrder(prescriptionArea.getText(), chckbxBloodTest.isSelected(), chckbxUrineTest.isSelected(),
						chckbxImmunizationTest.isSelected(), followUpArea.getText(), otherArea.getText());
				presenter.setDoctorsOrder(userId, currentTr.getDate(), currentTr.getTime(), newOrder);
				
				JOptionPane.showMessageDialog(null, "Successfully updated Doctor's Order",  // $codepro.audit.disable stringLiterals
							"Error", JOptionPane.INFORMATION_MESSAGE); // $codepro.audit.disable stringLiterals
				
				setVisible(false);
				dispose();
			}
		});
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				setVisible(false);
				dispose();
			}
		});
		btnBack.setBounds(186, 489, 89, 23);
		contentPane.add(btnBack);
	}
}
